package org.brisling.oa.queryService;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletResponse;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.common.util.JsonUtil;
import org.brisling.common.util.MapUtil;
import org.brisling.oa.domain.OAExecPurchaseSmall;
import org.brisling.oa.domain.OAGeneral;
import org.brisling.oa.queryService.base.AbstractOAQueryService;
import org.brisling.sysman.domain.Permissions;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * <p>Title: QueryOAGeneralService</p>
 * <p>Description: oa通用查询服务类</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年7月7日 上午10:22:55
 */
@RestController
//映射路径
@RequestMapping("/oaGeneral/query")
                                                                  //实体泛型类型
public class QueryOAGeneralService extends AbstractOAQueryService<OAGeneral> {
	
	
	
	@Autowired
	private	ShiroSecurityService shiroSecurityService;
	@Autowired
	private QueryDictionaryService dicService;
	
	private static final String DEPT_CODE = "apply_deptid";
	
	private static final String QUERY_BIZ_ALL_CODE = "query_oa_all";
	
	
	//导出excel抬头
	private String exportTitle = "";
	
	//导出excel配置项名称
	private String exportConfig = "";
	
	public QueryOAGeneralService(){
		this.moduleName = ConstSysParam.MODULE_OA_GENERAL;
		this.subSystem = ConstSysParam.SUBSYSTEM_OA;
		this.tableName = this.moduleName;
		this.clazz = OAGeneral.class;
		
		typeMap = this.getTypeMap(clazz);
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	/**
	 * 
	 * 获取用户管理的任务集合
	 * @param userid	用户id
	 * @param bizcode	业务代码
	 * @param page		页号
	 * @param rows		行数
	 * @return
	 */
	@RequestMapping("/getUserManagedTaskDomain.action")
	public ListDomain<OAGeneral> getUserManagedTask(@RequestBody(required=false) String body){		
						
		ListDomain<OAGeneral> lists = new ListDomain<OAGeneral>();	
		
		Map<String,Object> map = JsonUtil.body2Class(body, Map.class);
		String userid = MapUtil.getMapData(map, "userid");
		String bizcode = MapUtil.getMapData(map, "bizcode");
		
		if(userid!=null && bizcode!=null){	
			String sqlid = "oa_userTask_list";				
			lists = this.getTask(map, sqlid);
		}		
		return lists;
	}
	
	/**
	 * 获取所有流程记录
	 * @param userid	用户id
	 * @param bizcode	业务编码
	 * @param page		页号
	 * @param rows		行数
	 * @return
	 */
	@RequestMapping("/getAllTaskListDomain.action")
	public ListDomain<OAGeneral> getAllTask(@RequestBody(required=false) String body){
		
		
		ListDomain<OAGeneral> lists = new ListDomain<OAGeneral>();
		
		Map<String,Object> conditionMap = JsonUtil.body2Class(body, Map.class);
		
		
		String userid = null;
		//参数判断
		if(conditionMap==null || !conditionMap.containsKey("userid")){
			return lists;
		}else{
			userid = MapUtil.getMapData(conditionMap, "userid");
		}
		
		String sqlid = "oa_all_task";
		
		String bizcode = MapUtil.getMapData(conditionMap, "bizcode");
		//获取业务完整编码
		String fullBizcode = null;			
		if(bizcode!=null && SysConfig.getFlowBizcodeMap()!=null){
			Permissions p =  SysConfig.getFlowBizcodeMap().get(bizcode);
			if (p!=null && p.getFullBizcode()!=null){				
				conditionMap.put("fullBizcode", p.getFullBizcode());
			}
		}
		
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, QUERY_BIZ_ALL_CODE)){
			//非超级管理科室				
			
			if(!this.isBizManageDept(userid, fullBizcode)){
				//非业务管理科室
				if(!this.isManageJob(userid)){
					//非管理岗人员，添加人员过滤
					conditionMap = this.addUsridConditionToMap(userid, conditionMap, USER_CODE);
				}else{
					//管理岗人员，添加部门过滤
					conditionMap = this.addDeptConditionToMap(userid,conditionMap, DEPT_CODE);
				}				
			}
		}
		//执行查询
		lists = getTask(conditionMap,sqlid);
		return lists;
	}
	/**
	 * 
	 * 获取任务集合
	 * @param map	用户id
	 * @param sqlid	业务代码	 * 
	 * @return
	 */
	
	 public ListDomain<OAGeneral> getTask(Map<String,Object> map,
			String sqlid){		
		
		ListDomain<OAGeneral> lists = new ListDomain<OAGeneral>();
		
		if(map==null || sqlid==null ){
			return lists;
		}
		
		//默认取管理部门查询权限sql
		String sql = dicService.getSqlByBizcode("sqlid");			
			
		String page = MapUtil.getMapData(map, "page");
		String rows = MapUtil.getMapData(map, "rows");
			
		Integer pa = page==null?ConstSysParam.PAGE:0;
		Integer ps = rows==null?ConstSysParam.ROWS:10;
		lists = this.getPagedListDomain(pa, ps, sql)	;
				
		return lists;
	}
	
	
	
	
	
	/**
	 * 根据id查找记录
	 * @param id	业务记录id
	 * @return 		业务记录 
	 */
	@RequestMapping("/getOne.action")
	public OAGeneral getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据业务唯一编码查找业务记录
	 * @param identify	业务唯一编码
	 * @return 			业务记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public OAGeneral getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	/**
	 * 根据业务唯一编码查找业务信息
	 * @param identify	业务唯一编码
	 * @return 			业务记录
	 */
	@RequestMapping("/getOneByField.action")
	public OAGeneral getOneByField(@RequestParam(value="field",required=false) String field,
			@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByIdentify(field,fieldVal);
	}
	
	/**
	 * 根据指定字段名及字段值查找记录
	 * @param field		字段名
	 * @param fieldVal	字段值
	 * @return
	 */
	@RequestMapping("/getOneByBizTable.action")
	public OAGeneral getOneByBizTable(@RequestParam(value="field",required=false) String field,
			@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByTable(field,fieldVal);
	}
	
	/**
	 * 返回流程记录流程实例的执行记录
	 * @param body	包含procInstId在内的参数信息
	 * @return
	 */
	
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<Object> getHistoricByProcInstId(@RequestBody(required=false) String body){
		
		String sql = dicService.getSqlByBizcode("flow_historics");				
		return super.getHistoricByProcInstId(body, sql);
	}
	
	public String generateBizno(){
		return super.generateBizno();
	}
	/**
	 * 查询窗口满意度异常数据
	 * @return
	 *
	 */	
	
	public List<OAGeneral> getWindowUnSatisfy(){		
		
		String sql =dicService.getSqlByBizcode("duties_abnormal_OAGeneral");		
		
		try {
			return super.getClassedAllList(sql, OAGeneral.class);
		} catch (DangerousOperatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 	
		return null;
	}

	public List<OAGeneral> getAllList(String sql){
		return super.getAllList(sql);
	}
	
	/**
	 * 导出excel文档
	 * @param body
	 * @param response
	 */
	@RequestMapping("/exportExcel.action")
	public void exportExcel(@RequestBody(required=false) String body,HttpServletResponse response){
		super.exportExcel(body, response, exportTitle,exportConfig);
	}
	
	/**
	 * 导出审批完成的excel文档
	 * @param body
	 * @param response
	 */
	@RequestMapping("/exportExcelFinished.action")
	public void exportExcelFinished(@RequestBody(required=false) String body,HttpServletResponse response){
		super.exportExcelFinished(body, response, exportTitle,"注意：当前只能导出已经审批完成的数据",exportConfig);
	}
}
